package org.zhu.service_merge_v2.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.zhu.service_merge_v2.service.UsersCommandService;
import org.zhu.service_merge_v2.service.UsersQueryService;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;


/**
 * Created by zhu yingzhi on 2017/8/30.
 */
@RestController
@RequestMapping(value = "/")
public class UserController {

    private UsersCommandService usersCommandService;
    private UsersQueryService usersQueryService;
    Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    public UserController(UsersCommandService usersCommandService,UsersQueryService usersQueryService){
        this.usersCommandService = usersCommandService;
        this.usersQueryService = usersQueryService;
    }

    @GetMapping(value = "command/hello")
    public String hiCommand(){
        return usersCommandService.sayCommandHello();
    }

    @GetMapping(value = "hello")
    public String hello(){
        return "hello";
    }

    @GetMapping(value = "query/hello")
    public String hiQuery(){
        return usersQueryService.sayQueryHello();
    }

    /**
     * 并发性编程测试
     * @return
     */
    @GetMapping(value = "not/future")
    public String notfuture(){
        String name = usersQueryService.sayQueryName()+usersCommandService.sayCommandName();
        return name;
    }

    @GetMapping(value = "has/future")
    public String hasfuture() throws ExecutionException, InterruptedException {
        CompletableFuture<String> queryname = CompletableFuture.supplyAsync(()->usersQueryService.sayQueryName());
        CompletableFuture<String> commandname = CompletableFuture.supplyAsync(()->usersCommandService.sayCommandName());
        String name = queryname.get() + commandname.get();
        return name;
    }
}
